import {
	defineStore
} from 'pinia'
import {
	convertToTree,
	flattenTree,
	handleCheckBoxChecked,
	handleSelectedPopup,
	mapToArr,
	selectedTypeNumComputed,
	SELECT_PAGE_TYPE
} from '../../utils/util'


import _ from 'lodash'
import {
	FormDeptStoreKey,
} from '../sotreKey';

export const useDeptStore = defineStore('deptStore', {

	state: () => ({
		deptStoreKey: FormDeptStoreKey,
		storeKey: '',
		// checkedValue: [],
		deptTeamUserList: [],
		deptList: [],
		pathData: [],
		// 选择的数据类型展示
		// selectedTypeValue: SELECT_PAGE_TYPE,
		// 用于切换时候的key 处理
		key: '',
		// 已选弹窗列表数据
		selectedMutiplatePopupDataList: {},

		// ------------
		list: [],
		rewriteSourceData: [],
		viewListData: [],
		copyViewListData: [],
	}),
	actions: {
		clearDeptStoreKeyValue(key, value) {
			this.deptStoreKey[key].value = value || []
		},
		onRemovePopupListData(index) {
			this.deptStoreKey[this.storeKey].value.splice(index, 1)
		},
		setCheckedValue_CheckBox(data) {
			const val = handleCheckBoxChecked(data.value, data.checked, this.deptStoreKey[this.storeKey].value)

			this.selectedMutiplatePopupDataList = val

			this.setStoreKeyValue(val, null)


		},
		setCheckValue_Radio(data) {
			console.log(data, 'da');
			// this.checkedValue = data
			this.setStoreKeyValue([data], null)
		},
		setStoreKeyValue(data, label, key) {
			const k = key || this.storeKey
			this.deptStoreKey[k].value = data
			this.deptStoreKey[k].label = label || ''
		},


		setInit(data) {
			console.log(data, 'data');
			this.list = data.list
			this.rewriteSourceData = data.rewriteSourceData
			this.viewListData = data.viewListData
			this.copyViewListData = data.copyViewListData
			// this.checkedValue = this.deptStoreKey[data.storeKey].value
			this.storeKey = data.storeKey
			this.pathData = []

		},
		setPathData(val) {
			console.log(val, 'val');
			this.pathData.push(val)

		},
		setViewListData(val) {
			this.key = _.uniqueId('cell_')
			this.viewListData = val
		},
		onPathItem({
			value,
			index
		}) {
			this.pathData.splice(index + 1)
			const list = _.filter(this.copyViewListData, (item) => item.value === value)
			this.setViewListData(list[0].children, )
		},
		onAll() {
			this.setViewListData(this.copyViewListData)
			this.pathData = []
		},
		delValue(index, key) {
			if (this.deptStoreKey[key].value instanceof Array) {
				this.deptStoreKey[key].value.splice(index, 1)
			}

		},
		clearValue(key) {
			this.deptStoreKey[key] = {
				key,
				value: null,
				label: null,
			}
			this.checkedValue = null
		},
		onChangeKeywords: _.debounce(function(e) {
			const {
				value
			} = e.detail
			if (value) {
				const filters = _.filter(this.rewriteSourceData, (item) => {
					return item._name.indexOf(value) > -1
				})
				console.log(filters, 'filters');
				this.setViewListData(filters)
			} else {
				this.setViewListData(this.copyViewListData)

			}
		}, 500),
		onSelectAll() {
			const values = _.map(this.viewListData, 'value');

			this.setStoreKeyValue(values, null)
			// const values = _.map(this.viewListData, '_value');
			// this.checkedValue = values
			// this.selectedTypeValue = selectedTypeNumComputed(this.checkedValue, this.copyViewListData)
			// this.selectedMutiplatePopupDataList = handleSelectedPopup(this.copyViewListData, this
			// 	.checkedValue)

		},
		setSelectedMutiplatePopupDataList(key, index, value) {
			const subList = this.selectedMutiplatePopupDataList[key]
			subList.splice(index, 1)
			Object.assign(this.selectedMutiplatePopupDataList, {
				[key]: subList
			})

			const arr = mapToArr(this.selectedMutiplatePopupDataList)
			const values = _.map(arr, 'value')
			this.checkedValue = values
			// this.selectedTypeValue = selectedTypeNumComputed(values, this.copyViewListData)


		}
	},

	getters: {
		// 平铺
		flattenCopyViewListData() {
			return flattenTree(this.copyViewListData)
		},
		// 获取选中值的集合
		checkedCollection() {
			// const n = []
			// const the = this
			// _.forEach(the.flattenCopyViewListData, function(item) {
			// 	if (_.includes(the.checkedValue, item._value)) {
			// 		n.push(item)
			// 	}
			// })
			// return n
		},
		// this.checkedValue = this.deptStoreKey[this.storeKey].value

		checkedValue() {
			return this.deptStoreKey[this.storeKey].value || JSON.stringify({})
		},
		selectedTypeValue() {
			return {
				dept: {
					text: '科室',
					num: this.deptStoreKey[this.storeKey].value.length
				},
			}

		},
		currentValueLabel() {
			if (this.checkedValue instanceof Array) {
				console.log(this.checkedValue);
				if (this.checkedValue.length) {

					return JSON.parse(this.checkedValue[0]).label
				}
				return ''
			} else if (typeof this.checkedValue === 'string') {
				return JSON.parse(this.checkedValue).label
			}
			return ''
		}


	},
	persist: true,
})